import java.util.ArrayList;

/**
 * Represents a product
 * @author Emiliano Marin
 *
 */
public class Prodotto {
	
	private String codice = null; 
	private String nome = null; 
	private int quantita = 0; 
	
	private String taglia = "N/A"; 
	private ArrayList<String> taglie = null;
	
	private float prezzo = -1;
	
	/**
	 * Sets the article's bar code
	 * @param codice A string character representing the bar code of the product
	 */
	public void setCodice(String codice) {
		
		this.codice = codice;
		
		if (codice.length()>0 && (MagazzinoDB.codiceEsiste(codice))>0) { //If the article's code exists in the database
			//Retrieves name
			setNome(MagazzinoDB.getNome(codice));
			//Retrieves variant(s) 
			ArrayList<String> taglie = MagazzinoDB.getTaglie(codice);
 
			if (taglie.size()==1) {	//If only one variant 
				setTaglia(taglie.get(0));
				taglie = null;
			} else { //If more variants
				this.setTaglie(taglie);
				taglia = null;
			}
		} else { //New article's code
			setNome(null);
		}
	}
	
	/**
	 * Gets the article's bar code
	 * @return A String representing the article's bar code
	 */
	public String getCodice() {
		return codice;
	}
	
	/**
	 * Sets the name of the article
	 * @param nome A String representing the name of the product
	 */
	public void setNome(String nome) {
		this.nome = nome;
	}
	
	/**
	 * Gets the name of the article
	 * @return A String representing the article's name
	 */
	public String getNome() {
		return nome;
	}
	
	/**
	 * Sets the quantity of the article.
	 * The new specified quantity is summed to the existing quantity available in the database, if any. 
	 * If the article doesn't exist sets the quantity available in the database.
	 * @param quantita A string representing the quantity as a numeric value 
	 */
	public void setQuantita(String quantita) { 
		try {
			this.quantita = Integer.parseInt(quantita) + MagazzinoDB.getQuantita(this.codice, this.taglia);
		} catch (NumberFormatException e) {
			setQuantita(MagazzinoDB.getQuantita(this.codice, this.taglia));
		}
		
	}
	
	/**
	 * Sets the quantity of the article. 
	 * The new specified quantity is summed to the existing quantity available in the database, if any.
	 * @param quantita An integer representing the quantity of the article
	 */
	public void setQuantita(int quantita) {
		this.quantita = quantita;
	}
	
	/**
	 * Gets the quantity of the article.
	 * @return
	 */
	public int getQuantita() {
		return quantita;
	}
	
	/**
	 * Sets the variant of the article.
	 * @param taglia A String representing the variant of the article. If null or empty taglia will be assigned value "N/A".
	 */
	public void setTaglia(String taglia) {
		if (taglia == null) 
			taglia = "N/A";
		if (taglia.length() == 0) {
			taglia = "N/A";	
		}
		this.taglia = taglia;
		if (codice != null && taglia != null) {
			this.setPrezzo(MagazzinoDB.getPrezzo(this.codice, this.taglia));
			this.setQuantita(MagazzinoDB.getQuantita(this.codice, this.taglia));
		} else {
			this.setPrezzo(-1);
			this.setQuantita(0);
		}
	}
	
	/**
	 * Retrieves the variant of the product
	 * @return A String representing the variant of the product
	 */
	public String getTaglia() {
		return taglia;
	}

	/**
	 * Sets an Array List containing all variants for a given article's bar code 
	 * @param taglie A ArrayList containing the variants for a given article's bar code 
	 */
	public void setTaglie(ArrayList<String> taglie) {
		this.taglie = taglie;
	}

	/**
	 * Gets an Array List containing all the variants for the article.
	 * @return An Array List containing all variants for the article.
	 */
	public ArrayList<String> getTaglie() {
		return taglie;
	}

	/**
	 * Sets the price of the article.
	 * @param prezzo A float value representing the price of the article.
	 */
	public void setPrezzo(float prezzo) {
		this.prezzo = prezzo;
	}
	
	/**
	 * Sets the price of the article. Created for usage with user form's JTextField.
	 * @param prezzo A String value representing the price of the article expressed as a numeric value.
	 * If the passed String is cannot be casted in Float then -1 will be assigned as price, to avoid the application from saving the article.  
	 * If the price is not specified 0 will be used as default price.
	 *  
	 */
	public void setPrezzo(String prezzo) {
		if (prezzo.length()==0) {
			prezzo = "0";
		}
		try {
			this.prezzo = Float.parseFloat(prezzo);
		} catch (NumberFormatException e) { 
			setPrezzo(-1);
		}
		
	}

	/**
	 * Gets the price of the article.
	 * @return A float representing the price of the article. 
	 */
	public float getPrezzo() {
		return prezzo;
	}
	
	/**
	 * Saves the article in the database.
	 * @return A boolean value true if the operation was successful or false if not.
	 */
	public boolean salvaProdotto () {
		if (controlloValori()) {
			return MagazzinoDB.inserisciArticolo(codice, nome, quantita, taglia, prezzo);
		} else {
			return false;
		}
	}
	
	/**
	 * Check whether the values for bar code, name, variant, price and quantity can be used. 
	 * @return A boolean value true if the check is ok or false if not.
	 */
	public boolean controlloValori() { 
		if(this.codice != null && this.nome != null && this.taglia != null && this.prezzo >= 0 && this.quantita >= 0) {
			return true;
		}
		return false;
	}
}